#include <stdio.h>
#include <stdlib.h>

/*
 * A tool to get the file's length.
 * Compile : gcc get_file_len.c -o get_file_len
 * Usage : ./get_file_len file1_path
 * 
 * int fseek(FILE *stream, long offset, int whence);
 * long ftell(FILE *stream);
 * void rewind(FILE *stream);
 *
 * The  rewind()  function equivalent to:
 * (void) fseek(stream, 0L, SEEK_SET)
 */
int main(int argc, char *argv[])
{
	if (argc != 2) {
		fprintf(stderr, "Usage : %s file1_path\n", argv[0]);
		exit(1);
	}

	FILE *fp1 = NULL;

	if ((fp1 = fopen(argv[1], "r")) == NULL) {
		perror("fopen()");
		exit(1);
	}
	
	// fseek(fp1, 0L, SEEK_END) set the position indicator to the end of file
	fseek(fp1, 0L, SEEK_END);
	// ftell(fp1) tells the current value of the file position indicator
	printf("The num of char in %s is %ld\n", argv[1], ftell(fp1));

	fclose(fp1);

	exit(0);
}
